<%@ page import="com.oval.util.JspHelper" %>
<%@ page import="java.util.* " %> 
<%@ page import="java.lang.* " %> 
<%@ page import="java.io.* " %>
<%@ page import="com.oval.foundation.engine.*,com.oval.foundation.util.* " %> 
<%@ page import="com.oval.foundation.metadata.engine.*" %> 

<%@ page contentType = "text/html;charset=UTF-8" %>
<% Vector v = new Vector();%>
<% 
  String sqlString = request.getParameter("sqlString");
  String vtableName = request.getParameter("VirtualTable");
  String inQLName = request.getParameter("QLName");
  String testOrder = request.getParameter("order");
  String searchParam = request.getParameter("searchParam");
  HashMap ParamInput = (HashMap)request.getAttribute("ParamInput");
  UserInfo userInfo = (UserInfo)session.getAttribute(Constants.SESSION_USERINFO_NAME);
  if(userInfo==null)
    userInfo = new UserInfo(session.getId());
  if(ParamInput==null)
  {
    ParamInput = new HashMap();
  }
  
  String testSql = "";
  if(testOrder!=null&&testOrder.equals("changeVtable"))
  {
    
  }
  else
  {
    if(testOrder!=null&&testOrder.equals("search"))
    {
      //System.out.println("--------------------------------------------------------");
      OvalSearchAPI search = new OvalSearchAPI();
      search.setBI(userInfo,new Long(Logger.NO_JOB),new Integer(0),null);
      ParamInput = search.getSearchParamList(vtableName,inQLName);
      HashMap ParamData = new HashMap();
      //System.out.println(ParamInput);
      
      Iterator ite = ParamInput.values().iterator();
      while(ite.hasNext())
      {
        OvalQLParam param = (OvalQLParam)ite.next();
        String Type = param.getType();
        String a_Param = request.getParameter(param.getName());
        Object value = null;
        if(a_Param!=null&&a_Param.trim().length()!=0)
        {
          //System.out.println(param.getName());
          //System.out.println(a_Param);
          if(Type.equalsIgnoreCase("Variant"))
          {
            value = com.oval.foundation.util.OvalEncode.GBKToIso(a_Param);
          }
          if(Type.equalsIgnoreCase(com.oval.foundation.util.Constants.COLTYPE_VARCHAR))
          {
            value = com.oval.foundation.util.OvalEncode.GBKToIso(a_Param);
          }  
    
          if(Type.equalsIgnoreCase(com.oval.foundation.util.Constants.COLTYPE_INTEGER))
          {
            value = new Integer(a_Param);
          }
    
          if(Type.equalsIgnoreCase(com.oval.foundation.util.Constants.COLTYPE_DOUBLE))
          {
            value = new Double(a_Param);
          }
    
          if(Type.equalsIgnoreCase(com.oval.foundation.util.Constants.COLTYPE_DATE))
          {
            value = java.sql.Date.valueOf(a_Param);
          }
          ParamData.put(param.getName(),value);
        }  
      }
      search = new OvalSearchAPI();
      search.setBI(userInfo,new Long(Logger.NO_JOB),new Integer(0),null);
      //System.out.println(ParamData);
      session.setAttribute("VT_S_ParamData",ParamData);
      testSql = search.getSearchSQL(vtableName,inQLName,ParamData);
      //System.out.println(testSql);
    }
    else
    {
      if(testOrder!=null&&testOrder.equals("changeQL"))
      {
        if(inQLName!=null)
        {
          OvalSearchAPI search = new OvalSearchAPI();
          search.setBI(userInfo,new Long(Logger.NO_JOB),new Integer(0),null);
           testSql = search.getSearchSQL(vtableName,inQLName);
           ParamInput = search.getSearchParamList(vtableName,inQLName);
        }
      }
      else
      {
        OvalSQL m_sql=new OvalSQL();
 
        if(sqlString !=null)
        {
   
          m_sql.open(com.oval.foundation.util.OvalEncode.GBKToIso(sqlString));
          String columnViewName = null;
          OvalVTable m_vtable = com.oval.foundation.container.VTableContainer.findVTableMeta(vtableName);
          if(inQLName!=null)
          {
            columnViewName = m_vtable.getOvalQLByName(inQLName).getColumnViewName();
          }
          boolean isView = true;
          String[] ColumnView = null;
          if(columnViewName==null)
          {
             isView = false;
          }  
          if(isView)
          {
             ColumnView = m_vtable.findColumnView(columnViewName);
          }  
    
          HashMap ParamData = new HashMap();
          if(session.getAttribute("VT_S_ParamData")!=null)
          {
            ParamData = (HashMap)session.getAttribute("VT_S_ParamData");
            session.removeAttribute("VT_S_ParamData");
          } 
           while(true)
           {
            Iterator eCols=m_vtable.getColumnNames();
            if(isView)
            {
             if(ColumnView!=null)
             {
              HashMap temp = new HashMap();
              for(int i =0;i<ColumnView.length;i++)
              {
                temp.put(new Integer(i),ColumnView[i]);
              }
                eCols = temp.keySet().iterator(); 
              }    
            }   
           HashMap row = m_sql.getNextRow(vtableName,eCols,ParamData);
            if(row == null)
            break;
          v.add(row);
         }
    
             m_sql.close();      
    
        }
      }
    }
  }
  
  
  
  
  Iterator iteTables = com.oval.foundation.container.VTableContainer.getVTableMetas();
  Iterator enumQLNames = null;
  if(vtableName!=null)
  {
    OvalVTable selectedTable = com.oval.foundation.container.VTableContainer.findVTableMeta(vtableName);
    enumQLNames = selectedTable.getOvalQLNames();
  }
  request.setAttribute("ParamInput",ParamInput);
%>

<script>
function SelectSingle(formObj, name) 
{ for(var i=0;i < formObj.length; i++)
  { if(formObj.options[i].value==name||Number(formObj.options[i].value)==Number(name)) 
    { formObj.selectedIndex=i;
      return;
    }
  }
  formObj.selectedIndex=0;
}

function changeVTable()
{
  if(document.testSQLForm.VirtualTable.value=="-1")
  {
    return false;
  }
  document.testSQLForm.order.value="changeVtable";  
  document.testSQLForm.submit();
  
}

function changeQL()
{
  if(document.testSQLForm.QLName.value=="-1")
  {
    return false;
  }
  document.testSQLForm.order.value="changeQL";  
  document.testSQLForm.submit();
  
}

function isEmpty(s)	<%--[SCRIPT]  Is the string s empty? Empty:return true.  [SCRIPT]--%>
{
	if((s == null) || (s.length == 0) || (s == ""))
	{
	  return true;
	}
	var is = 1;
	for(var i=0;i<s.length;i++)
	{
	  if(s.charAt(i)!=' '&&s.charAt(i)!='\r'&&s.charAt(i)!='\n')
	  {
	    is = 0;
	  }
	}
	if(is==0)
	{
    return false;	
	}
	else
	{
	  return true;
	}
}
function search()
{
  if(isEmpty(document.testSQLForm.sqlString.value))
  {
    return false;
    
  }
  
  document.testSQLForm.order.value="search";
  document.testSQLForm.submit();
}

</script>
<form name="testSQLForm" method="post" action="consoleContent.jsp">

<input type="hidden" name="contentType" value="VirtualTableTest">
<input type="hidden" name="order" value="">

<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr> 
    <td rowspan="3" bgcolor="999999" width="1"></td>
    <td bgcolor="999999" height="1"></td>
    <td rowspan="3" bgcolor="999999" width="1"></td>
  </tr>
  <tr> 
    <td class="td_bg_row">
      <table width="100%" border="0" cellspacing="0" cellpadding="2">
        <tr> 
          <td width="20%"> 
            <table width="100%" border="0" cellspacing="6" cellpadding="2">
              <tr> 
                <td bgcolor="ECECEC">
                  Select VirtualTable
                </td>
              </tr>
              <tr>
                <td bgcolor="ECECEC">
		  <select name="VirtualTable" onchange="javascript:changeVTable()">
    		    <option value="-1">
    		      --------------
    		    </option>
    		  <% while (iteTables.hasNext())
                     {
                       String tableName = ((OvalVTable)iteTables.next()).getTableName();
                       //System.out.println("searchParam != null"+searchParam);
                       //System.out.println("tableName.indexOf(searchParam.trim()) != -1"+(tableName.indexOf(searchParam.trim()) != -1));
                       if(searchParam != null && tableName.indexOf(searchParam.trim()) == -1)
                       {
                       	 continue;
                       }
                       %>  
                    <option value=<%=JspHelper.filterNull( tableName )%>>
                       <%=JspHelper.filterNull( tableName )%>
                    </option>
                       <% 
                  		
                     } %>
                  </select>
                </td>
              </tr>
              <tr>
                <td bgcolor="ECECEC">
		  Select QL
		</td>
              </tr>
              <tr>
                <td bgcolor="ECECEC">
                  <select name="QLName" onchange="javascript:changeQL()">
                    <option value="-1">
                      --------------
                    </option>
                 <% while (enumQLNames!=null&&enumQLNames.hasNext())
                    {
                      String QLName = enumQLNames.next().toString();
                      %>  
                    <option value="<%=JspHelper.filterNull( QLName )%>">
                      <%=JspHelper.filterNull( QLName )%>
                    </option>
                 <% } %>
                  </select>
                </td>
              </tr>
              
  <script language="javascript">
    SelectSingle(testSQLForm.VirtualTable, "<%=JspHelper.filterNull( vtableName )%>")
  </script>
 
   
  <script language="javascript">
    SelectSingle(testSQLForm.QLName, "<%=JspHelper.filterNull( inQLName )%>")
  </script>
  <% if(ParamInput.size()!=0)
     { 
       Iterator it = ParamInput.values().iterator();
       while(it.hasNext())
       {%>
  <tr>
    <td bgcolor="ECECEC">
       <%
         OvalQLParam oqp = (OvalQLParam)it.next();
         String dvalueType = "Other";
         if(oqp.getValue() instanceof Integer)
           dvalueType = "Integer";
         else if(oqp.getValue() instanceof String)
           dvalueType = "String";
       %>
         <font color="blue"><%=JspHelper.filterNull(  oqp.getName() )%></font>&nbsp; <b><%=JspHelper.filterNull( oqp.getType() )%>:</b>
     </td>
  </tr>
  <tr>
    <td>
      default value:&nbsp;<b><%=JspHelper.filterNull( oqp.getValue())%></b>
    </td>
  </tr>
  <tr>
    <td>
      default value Type:&nbsp;<b><%=JspHelper.filterNull( dvalueType )%></b>
    </td>
  </tr>
  <tr>
     <td bgcolor="ECECEC">   
        <input type="text" name="<%=JspHelper.filterNull(  oqp.getName() )%>">
    <% } %>
    </td>
  </tr>
  <% } %>
</table>
</td>
<td valign="top" rowspan="1000" class="content-text-gray1">
  <textarea cols="115" rows="25" name="sqlString" >
   <%=JspHelper.filterNull(testSql)%>
  </textarea>
  <p align="right"> <input type="submit"><input type="submit" value="search" onclick="javascript:search();return false;"></p>

</td>
</tr>
</table>
 
  </tr>
  <tr> 
    <td bgcolor="999999" height="1"></td>
  </tr>
  <tr> 
   <td class="td_bg_row" height="20" colspan="3">&nbsp; </td>
  </tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr> 
    <td>&nbsp;</td>
  </tr>
</table>
</FORM> 



<table width="100%" border="0" cellspacing="1" cellpadding="2" bgcolor="#999999">
<%
boolean firstRow=true;
HashMap hashInfo=new HashMap();
//System.out.println("size is "+v.size());
for(int i=0;i<v.size();i++)
{
  hashInfo=(HashMap)v.elementAt(i);
  if(hashInfo!=null)
  {
    if(firstRow==true)
    {
      out.println("<tr>");     
      Iterator iteInfo = hashInfo.keySet().iterator();
      while(iteInfo.hasNext())
      {
        out.println("<td class=\"td_title_bg\">"+iteInfo.next()+"</td>"); 
      }
      out.println("</tr><tr class=\"td_bg_row\">"); 
          
      Iterator iteCont = hashInfo.values().iterator();
      while(iteCont.hasNext())
      {
        out.println("<td class=\"td_center\"><div align=\"left\">"+iteCont.next()+"</div></td>"); 
      }
      out.println("</tr>");   
      firstRow=false; 
      
    } 
    else
    {
      out.println("<tr class=\"td_bg_row\">"); 
          
      Iterator iteCont = hashInfo.values().iterator();
      while(iteCont.hasNext())
      {
        out.println("<td class=\"td_center\"><div align=\"left\">"+iteCont.next()+"</div></td>"); 
      }
      out.println("</tr>");
    }
  }        
}
%>


</table>

